package com.wenan.ui.reports
{
	import com.amcharts.chartClasses.ChartScrollbar;
	import com.wenan.AppGlobal;
	import com.wenan.data.DataUtil;
	import com.wenan.data.PageInfo;
	import com.wenan.data.ParameterCollection;
	import com.wenan.data.ReportData;
	import com.wenan.data.StringUtil;
	import com.wenan.ui.controls.charts.Clumps;
	import com.wenan.ui.controls.charts.Histogram;
	import com.wenan.ui.controls.charts.Linear;
	import com.wenan.ui.controls.charts.Splashes;
	import com.wenan.ui.controls.charts.expand.AmChartSpecial;
	import com.wenan.ui.controls.charts.expand.AmGraphExpand;
	import com.wenan.ui.controls.charts.expand.CategoryAxisExpand;
	import com.wenan.ui.layout.NavigatorClientPanel;
	
	import flash.events.Event;
	import flash.events.MouseEvent;
	import flash.net.FileReference;
	
	import mx.collections.ArrayCollection;
	import mx.containers.Canvas;
	import mx.containers.VBox;
	import mx.controls.Alert;
	import mx.controls.Button;
	import mx.controls.Image;
	import mx.controls.ToggleButtonBar;
	import mx.core.UIComponent;
	import mx.events.ItemClickEvent;
	import mx.rpc.events.FaultEvent;
	import mx.rpc.events.ResultEvent;
	
	public class ReportClientPanel extends NavigatorClientPanel
	{
		[Embed("images/toggleButton/table.png")] 
		private var tableIcon:Class; 
		[Embed("images/toggleButton/column.png")] 
		private var columnIcon:Class; 
		[Embed("images/toggleButton/line.png")] 
		private var lineIcon:Class; 
		[Embed("images/toggleButton/point.png")] 
		private var pointIcon:Class; 
		[Embed("images/toggleButton/簇状图.png")] 
		private var clumpsIcon:Class; 
		[Embed("images/toggleButton/bg.png")] 
		private var backPic:Class; 
		
		private var _xFieldInfos:Array;
		private var _yFieldInfos:Array;
		private var vBox:VBox;
		private var clientCanvas:Canvas;
		private var buttonCanvas:Canvas;
		private var buttonBar:ToggleButtonBar;
		
		public function ReportClientPanel()
		{
			super();
			
			initilizeComponents();
		}

		public function get reportType():String
		{
			if (parameters)
			{
				return parameters.getByName("ReportType").value;
			}
			return null;
		}
		
		public override function getNavigatorPageInfos(pageInfos:Array):Array
		{
			var length:int = pageInfos.length;
			var lastPageInfo:PageInfo = pageInfos[length - 1] as PageInfo;
			if(lastPageInfo)
			{
				if(lastPageInfo.pageName == "Report")
				{
					pageInfos[length - 1] = pageInfo;
				}else
				{
					pageInfos.push(pageInfo);	
				}
				
			}
			
			return pageInfos;
		}
		
		public override function pageInfoChanged():void
		{
			createToggleButtonBar();
			changeReportPanel(null);	
		}
		
		private function initilizeComponents():void
		{
			vBox = new VBox();
			vBox.setStyle("verticalGap", 0);
			vBox.percentWidth = 100;
			vBox.percentHeight = 100;
			
			clientCanvas = new Canvas();
			clientCanvas.percentWidth = 100;
			clientCanvas.percentHeight = 100;
			vBox.addElement(clientCanvas);
			
			buttonCanvas = new Canvas();
			buttonCanvas.percentWidth = 100;
			buttonCanvas.height = 28;
			vBox.addElement(buttonCanvas);

			addElement(vBox);
		}
		
		/**
		 * 创建底部Tab页
		 * */
		private function createToggleButtonBar():void
		{
			var default_report_type:String = parameters.getValue("default_report_type");
			var other_report_type:String = parameters.getValue("other_report_type");
			var dataProvider:ArrayCollection = new ArrayCollection();
			if (default_report_type)
			{
				dataProvider.addItem(constructionDefaultReport(default_report_type));
			}
			if (other_report_type)
			{
				constructonOther_report_type(other_report_type, dataProvider);
			}
			if (dataProvider && dataProvider.length > 0)
			{
				buttonCanvas.removeAllElements();
				
				var image:Image = new Image();
				image.source = backPic;
				image.height = 28;
				
				buttonBar = new ToggleButtonBar();
				buttonBar.styleName = "buttonBarStyle";
				buttonBar.dataProvider = dataProvider;
				buttonBar.height = 28;
				buttonBar.addEventListener(ItemClickEvent.ITEM_CLICK, buttonClickHandler);	
				
				var export:Button = new Button();
				export.styleName = "ExportButton";
				export.addEventListener(MouseEvent.CLICK, exportReportHandler);
				export.right = 14;
				
				buttonCanvas.addElement(image);
				buttonCanvas.addElement(buttonBar);
				buttonCanvas.addElement(export);
			}
		}
		
		private function constructionDefaultReport(default_report_type:String):Object
		{
			var obj:Object = new Object();
			var label:String;
			var iconClass:Class;
			switch(default_report_type)
			{
				case "table":
					label = "表格";
					iconClass = tableIcon;
					break;
				case "column":
					label = "柱状图";
					iconClass = columnIcon;
					break;
				case "line":
					label = "线状图";
					iconClass = lineIcon;
					break;
				case "cluster":
					label = "簇状图";
					iconClass = clumpsIcon;
					break;
				case "scatter":
					label = "散点图";
					iconClass = pointIcon;
					break;
				default:
					break;
			}
			obj.label = label;
			obj.icon = iconClass;
			return obj;
		}
		
		private function constructonOther_report_type(other_report_type:String, array:ArrayCollection):void 
		{
			var reportArray:Array = other_report_type.split(",");
			if (reportArray && reportArray.length > 0)
			{
				for each (var name:String in reportArray)
				{
					array.addItem(constructionDefaultReport(name));
				}
			}
		}
		
		/**
		 * 页面索引发生变化。
		 */
		private function buttonClickHandler(event:ItemClickEvent):void
		{
			if (event.item)
			{
				changeReportPanel(event.item);
			}
		}
		
		/**
		 * 修改报表面板。
		 */
		private function changeReportPanel(item:Object):void
		{
			if (!buttonBar)
			{
				return;
			}
			if (item == null)
			{
				var itemList:ArrayCollection = buttonBar.dataProvider as ArrayCollection;
				if (itemList && itemList.length > 0)
				{
					item = itemList.getItemAt(0);					
				}
			}
			//移除所有客户区对象。
			clientCanvas.removeAllElements();
			//创建报表面板。
			if (item)
			{
				parameters.getByName("ReportType").value = item.label;
				
				var reportInfo:ReportInfo = getReportInfo();
				var reportPanel:UIComponent = reportInfo.createReportPanel();
				reportPanel.width = 755;
				reportPanel.percentHeight = 100;
				//在控件中增加报表控件。
				if (reportPanel)
				{
					clientCanvas.addElement(reportPanel);	
				}	
			}
		}
		
		private function getReportInfo():ReportInfo
		{
			var reportInfo:ReportInfo = null;
			switch(reportType)
			{
				case "表格":
					//记录中存在名称属性，其余为数值属性。记录的名称列在列表中，可以选择是否显示。
					//数值属性列网格中，可以选择是否显示。
					reportInfo = new DataReportInfo();
					break;
				case "柱状图":
					//柱状图。图标横向显示名称属性，纵向显示数据属性。数据属性可以单选是否显示。
					reportInfo = new ThroughoutReportInfo();
					break;
				case "簇状图":
					//柱状图。图标横向显示名称属性，纵向显示数据属性。数据属性可以复选是否显示。
					reportInfo = new ComprehensiveComparisonReportInfo();
					break;
				case "线状图":
					//柱状图。图标横向显示时间属性，纵向显示数据属性。数据属性可以复选是否显示。
					reportInfo = new TimeComparisonReportInfo();
					break;
				case "散点图":
					//散点图图。图标横向显示面积属性，纵向显示数据属性。数据属性可以复选是否显示。
					reportInfo = new HashpointReportInfo();
					break;
			}
			if (reportInfo)
			{
				reportInfo.reportData = new ReportData(parameters.getObj("dataProvider"), parameters.getObj("xSelectableFields") as ArrayCollection, parameters.getObj("ySelectableFields") as ArrayCollection, parameters.getObj("xDefaultField") as String, parameters.getObj("yDefaultFields") as ArrayCollection, parameters.getObj("chartStyle"), parameters.getObj("reportStyle") as ParameterCollection);
				//设置当前选中字段。
				reportInfo.xField = parameters.getValue("xField");//来自页面配置。
				reportInfo.yFields = StringUtil.fromFieldStr(parameters.getValue("yFields"));//来自页面配置。
				if (reportInfo.yFields == null || reportInfo.yFields.length == 0)
				{
					reportInfo.yFields = new Array(reportInfo.reportData.yDefaultFields);
				}	
			}
			return reportInfo;
		}
		
		protected function exportReportHandler(event:MouseEvent):void
		{
			AppGlobal.dataLayer.exportDataToExcel();
		}
	}
}